﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace businessLayer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "VerhuurService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select VerhuurService.svc or VerhuurService.svc.cs at the Solution Explorer and start debugging.
    public class VerhuurService : iVerhuurd
    {
        
        public List<Verhuurd> GetAlleVerhuur()
        {
            List<Verhuurd> VerhuurLijst = new List<Verhuurd>();
            SqlConnection myConn = null;
            Verhuurd alle = new Verhuurd();
            try
            {
                using (myConn = new SqlConnection(BPcars.GetConnection()))
                {
                    myConn.Open();

                    var cmd = new SqlCommand("Select * from Verhuurd", myConn);
                    var reader = cmd.ExecuteReader();



                    while (reader.Read())
                    {
                        int VoertuigID = Convert.ToInt32(reader["VoertuigID"]);
                        int filiaalnr = Convert.ToInt32(reader["FiliaalNr"]);
                        int klantnr = Convert.ToInt32(reader["KlantNr"]);
                        DateTime start = (DateTime)reader["Start"];
                        int duur = Convert.ToInt32(reader["duur"]);

                        Verhuurd k = new Verhuurd { VoertuigID = VoertuigID, filiaalnr = filiaalnr, klantnr = klantnr, start = start, duur = duur};
                        VerhuurLijst.Add(k);

                    }

                    reader.Close();
                    cmd.Dispose();




                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            return VerhuurLijst;
        }

       
        public bool InsertVerhuur(Verhuurd verhuring)
        {
            SqlConnection connection = null;
            string insert = "insert into Verhuurd (VoertuigID,FiliaalNr,Klantnr,Start,Duur)" +
                            "Values(@VoertuigID,@FililaalNr,@Klantnr,@Start,@Duur);";
            SqlCommand insertCommand = new SqlCommand(insert, connection);
            insertCommand.Parameters.AddWithValue("@VoertuigID", verhuring.VoertuigID);
            insertCommand.Parameters.AddWithValue("@FiliaalNr", verhuring.filiaalnr);
            insertCommand.Parameters.AddWithValue("@Klantnr", verhuring.klantnr);
            insertCommand.Parameters.AddWithValue("@Start", verhuring.start);
            insertCommand.Parameters.AddWithValue("@Duur", verhuring.duur);
            try
            {
                using (connection = new SqlConnection(BPcars.GetConnection()))
                {
                    connection.Open();
                    int count = insertCommand.ExecuteNonQuery();
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }

        public bool DeleteVerhuur(int id, DateTime st)
        {
            SqlConnection connection = new SqlConnection(BPcars.GetConnection());
            string delete = "DELETE FROM Verhuurd " +
                            "WHERE VoertuigId = @VoertuigId and Start = @Start"; 
            SqlCommand deleteCommand = new SqlCommand(delete, connection);
            deleteCommand.Parameters.AddWithValue("@VoertuigId", id);
            deleteCommand.Parameters.AddWithValue("@Start", st);
            try
            {
                connection.Open();
                int count = deleteCommand.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
